package cn.cmdr.service.ServiceImpl;

import cn.cmdr.mapper.PermissionMapper;
import cn.cmdr.mapper.RolePermissionMapper;
import cn.cmdr.pojo.Permission;
import cn.cmdr.pojo.RolePermission;
import cn.cmdr.service.PermissionService;
import cn.cmdr.vo.TreeNode;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service("permissionService")
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission>  implements PermissionService {
    @Resource
    private PermissionMapper permissionMapper;
    @Resource
    private RolePermissionMapper rolePermissionMapper;

    public List<TreeNode> getnodes(Integer pid,Integer roleId){
        List<Integer> perids=rolePermissionMapper.getPerIdByRoleId(roleId);
        List<TreeNode> nodes = new ArrayList<>();
        List<Permission> list = getPermissions(pid);
        for (Permission permission:list) {
            TreeNode treeNode = new TreeNode();
            treeNode.setId(permission.getId());
            treeNode.setTitle(permission.getName());
            //判断是不是最小的节点
            QueryWrapper queryWrapper1 = new QueryWrapper();
            queryWrapper1.eq("parent_id",permission.getId());
            Integer count=this.baseMapper.selectCount(queryWrapper1);
            if(perids.contains(permission.getId())&&count==0){
                treeNode.setChecked(true);
            }
            nodes.add(treeNode);
        }
        return nodes;
    }
    private List<Permission> getPermissions(Integer pid) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (pid == null){
            queryWrapper.isNull("parent_id");
        }else{
            queryWrapper.eq("parent_id",pid);
        }
        queryWrapper.orderByAsc("sort");
        return super.list(queryWrapper);
    }
    @Override
    public List<TreeNode> getTreeNode(Integer roleId) {
        List<TreeNode> nodes1 = this.getnodes(null,roleId);
        for (TreeNode node1:nodes1){
            List<TreeNode> nodes2 = this.getnodes(node1.getId(),roleId);
            node1.setChildren(nodes2);
            for (TreeNode node2:nodes2) {
                List<TreeNode> nodes3 = this.getnodes(node2.getId(),roleId);
                node2.setChildren(nodes3);
            }
        }
        return nodes1;
    }
    @Override
    public List<String> getPerCode(Integer roleId) {
        return baseMapper.getPerCodes(roleId);
    }

    @Override
    public List<Permission> getPerMenus() {
        Subject subject = SecurityUtils.getSubject();
        List<Permission> menus1 = this.getPermissions(null);
        menus1 = menus1.stream().filter(permission ->subject.isPermitted(permission.getPermCode())).collect(Collectors.toList());
        for (Permission p01:menus1){
            List<Permission> menus2 = this.getPermissions(p01.getId());
            menus2 = menus2.stream().filter(permission ->subject.isPermitted(permission.getPermCode())).collect(Collectors.toList());
            p01.setChildren(menus2);
        }
        return menus1;
    }

    @Override
    public void dogrant(Integer roleId,Integer[] nid) {
        //根据perid删除  中间表
        rolePermissionMapper.deleteById(roleId);
        for (Integer perid:nid){
            rolePermissionMapper.insert(new RolePermission(roleId,perid));
        }
    }
}
